<?php
/*
 *---------------------------------------------------------------------------
 *
 *                  T E N C E N T   P R O P R I E T A R Y
 *
 *     COPYRIGHT (c)  2008 BY  TENCENT  CORPORATION.  ALL RIGHTS
 *     RESERVED.   NO  PART  OF THIS PROGRAM  OR  PUBLICATION  MAY
 *     BE  REPRODUCED,   TRANSMITTED,   TRANSCRIBED,   STORED  IN  A
 *     RETRIEVAL SYSTEM, OR TRANSLATED INTO ANY LANGUAGE OR COMPUTER
 *     LANGUAGE IN ANY FORM OR BY ANY MEANS, ELECTRONIC, MECHANICAL,
 *     MAGNETIC,  OPTICAL,  CHEMICAL, MANUAL, OR OTHERWISE,  WITHOUT
 *     THE PRIOR WRITTEN PERMISSION OF :
 *
 *                        TENCENT  CORPORATION
 *
 *       Advertising Platform R&D Team, Advertising Platform & Products
 *       Tencent Ltd.
 *---------------------------------------------------------------------------
 */

/**
 * 所有controller的基类
 *
 * @package lib.core
 * @version JYController.class.php
 */
abstract class JYController {
    /**
     * @var array    用于页面替换的变量
     *
     * @access protected
     */
    protected $vars = array();

    /**
     * @var string    controller name
     *
     * @access protected
     */
    protected $controllerName;

    /**
     * @var string    action name
     *
     * @access protected
     */
    protected $actionName;

    /**
     * @var JYWebRequest    request实例
     *
     * @access private
     */
     protected $request;

    /**
     * 构造函数
     *
     * @access public
     * @param  string $controllerName     the controller name
     * @param  string $actionName           the action name
     *
     */
    public function __construct($controllerName, $actionName) {
        $this->controllerName = $controllerName;
        $this->actionName = $actionName;
        $this->request = JYDispatcher::getInstance()->getRequest();
    }

    /**
     * Get template path
     *
     * @return the template path
     */
    protected function getTemplatePath() {
        return $this->controllerName .'/'. $this->actionName . '.php';
    }

    /**
     * Render the template with Content-Type:text/xml header
     *
     * @param string $tpl     template file
     *
     * @return the response content
     */
    protected function renderXml($tpl=null) {
        header('Content-Type: text/xml; charset=utf-8');
        return $this->render('', $tpl, false);
    }

    /**
     * Render the template with Content-Type:application/x-javascript header
     *
     * @param string $tpl     template file
     *
     * @return the response content
     */
    protected function renderJs($tpl=null) {
        header('Content-Type: application/x-javascript; charset=utf-8');
        return $this->render('', $tpl, false);
    }

    /**
     * Render the template with layout
     *
     * @param string $tpl     template file
     *
     * @return the response content
     */
    protected function render($tpl=null) {
        $view = new JYView();
        return $view->render($this->vars, $tpl);
    }

    /**
     * Set array vars for rendering
     *
     * @param $key                the vars key
     * @param $value              the value of the key
     *
     * @return void
     */
    protected function __set($key, $value) {
        $this->vars [$key] = $value;
    }

    /**
     * Get array vars value used key
     *
     * @param  $key              the vars key
     * @return value
     */
    protected function __get($key) {
        return $this->vars[$key];
    }

    /**
     * Send http request to back location，需要2次请求
     *
     * @param string $alert             the alert message
     * @param string $url               default is TMConfig::Domain
     * @return string $content
     */
    public function alert($alert = "", $url="",$noCache = false) {
		if((boolean)$noCache) {
			header("Last-Modified: ". gmdate ("D, d M Y H:i:s", time()) . " GMT");
			header("Expires: ". gmdate ("D, d M Y H:i:s", time() - 3600) . " GMT");
			header("Cache-Control: private");
		}
		
	
        if (!empty($alert)) {
            $alertstr = "alert('" . $alert . "');\n";
        } else {
            $alertstr = "";
        }

        if (empty ($url)) {
            $gotoStr = "window.history.back();\n";
        } else {
            $gotoStr = "window.location.href='" . $url . "'\n";
        }

        $content = "\t<script language=javascript>\n\t<!--\n";
        if (!empty($alertstr)) {
            $content .= $alertstr;
        }

        if($url != "NONE") {
            $content .= $gotoStr;
        }
        $content .= "\t-->\n\t</script>\n";

        echo $content;
    }

    /**
     * 实现请求转发，对于用户来说不透明
     *
     * @param  string $controller      the forwarded controller name
     * @param  string $action          the forwarded action name
     * @return string $content         the content string
     */
    public function forward($controller, $action) {
        $dispatcher = JYDispatcher::createInstance();
        $dispatcher->forward($controller, $action);
    }

    /**
     * 实现链接跳转，需要多1次请求
     *
     * @param  string $url
     * @param  int $delay    跳转延时
     * @param  int $statusCode   跳转编码
     */
    public function redirect($url, $delay = 0, $statusCode = 302) {
        // redirect
        header("HTTP/1.1 {$statusCode}");
        header("Location: {$url}");
    }

    /**
     * 去执行指定controller中的action方法，如果方法不存在，则默认直接渲染页面
     *
     * @access public
     * @param string $controllerInstance   controller instance
     * @param string $funcName  action function name
     * @return string $content
     */
    public function execute($controllerInstance,$funcName) {
        try {
            if (method_exists($controllerInstance,$funcName)) {
                $content = call_user_func_array(array(&$controllerInstance, $funcName), array());
            } else {
                $content = $this->render();
            }
            return $content;
        } catch (JYException $te) {
            throw new Exception("Error in JYController::execute...");
        }
    }

    /**
     * 处理execute方法中捕获到的exception
     *
     * @param  TMException $exception   TMException实例
     * @return string $content
     * @throws TM404Exception
     */
    protected function exceptionHandle($exception) {
        return $exception->handle();
    }
    
	/*过滤请求
	 * 
	 */
	public function filterRequest(){
	    if(strpos($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']) !== 0) {
            echo "温馨提示：非法请求!"; 
	    	exit;
		}
	}
}
